package com.luoye_lj.dogcontroller.util;

import android.os.Build;
import android.util.Log;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.List;
import java.util.Properties;

import javax.activation.DataHandler;
import javax.mail.*;
import javax.mail.internet.*;
import javax.mail.util.ByteArrayDataSource;

public class EmailUtil {
    final static String sendAddress="ljsj_2009@sina.com";
    public static Session createSession() {
        //	账号信息
        String username = "ljsj_2009@sina.com";//	邮箱发送账号
        String password = "556720e0e3000488";//	邮箱授权码

        //	创建一个配置文件，并保存
        Properties props = new Properties();

        //	SMTP服务器连接信息
        //  126——smtp.126.com
        //  163——smtp.163.com
        //  qqsmtp.qq.com"
        props.put("mail.smtp.host", "smtp.sina.com");//	SMTP主机名

        //  126——25
        //  163——645
        props.put("mail.smtp.port", "25");//	主机端口号
        props.put("mail.smtp.auth", "true");//	是否需要用户认证
        props.put("mail.smtp.starttls.enale", "true");//	启用TlS加密

        Session session = Session.getInstance(props, new Authenticator() {
            @Override
            protected PasswordAuthentication getPasswordAuthentication() {
                // TODO Auto-generated method stub
                return new PasswordAuthentication(username, password);
            }
        });

        //  控制台打印调试信息
        session.setDebug(true);
        return session;
    }

    public static void sendTxt(String subject, String text, String toAddress) {
        try {
            //	创建Session会话
            Session session = createSession();

            //	创建邮件对象
            MimeMessage message = new MimeMessage(session);
            message.setSubject(subject);
            message.setText(text);
            message.setFrom(new InternetAddress(sendAddress));
            message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(toAddress));

            Log.i("TAG", "sendTxt: "+sendAddress);
            //	发送
            Transport.send(message);
            Log.i("TAG", "sendTxt: "+toAddress);
        } catch (AddressException e) {
            e.printStackTrace();
        } catch (MessagingException e) {
            e.printStackTrace();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void sendFile(String subject, String text, List<String> filePaths, String toAddress){
        try {
            //int kk=100/0;//测试错误
            //	创建会话
            Session session = createSession();

            //	创建邮件
            MimeMessage message = new MimeMessage(session);
            message.setFrom(sendAddress);
            message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(toAddress));
//            message.setRecipients(MimeMessage.RecipientType.CC, new InternetAddress[] {new  InternetAddress("抄送人邮箱"),new InternetAddress("抄送人邮箱")});
            message.setSubject(subject);

            Multipart multipart = new MimeMultipart();
            //	邮件主体
            BodyPart textPart = new MimeBodyPart();
            textPart.setContent(text,"text/html;charset=UTF-8");
            multipart.addBodyPart(textPart);
            //	邮件附件
            for(String filePath:filePaths) {
                BodyPart filePart = new MimeBodyPart();
                File file=new File(filePath);
                String fileName=file.getName();
                filePart.setFileName(fileName);
                //	提交附件文件
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                    filePart.setDataHandler(new DataHandler(new ByteArrayDataSource(Files.readAllBytes(Paths.get(filePath)), "application/octet-stream")));
                }
                multipart.addBodyPart(filePart);
            }

            //	将邮件装入信封
            message.setContent(multipart);

            Transport.send(message);
        } catch (AddressException e) {
            e.printStackTrace();
        } catch (MessagingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void sendImage(String subject,String text, String filePath,String imageName, String toAddress){
        try {
            //	创建会话
            Session session = createSession();
            //	创建邮件
            MimeMessage message = new MimeMessage(session);
            message.setFrom(sendAddress);
            message.setRecipient(MimeMessage.RecipientType.TO, new InternetAddress(toAddress));
//            message.setRecipients(MimeMessage.RecipientType.CC, new InternetAddress[] {new  InternetAddress("抄送人邮箱"),new InternetAddress("抄送人邮箱")});
            message.setSubject(subject);

            //	邮件主体
            BodyPart textPart = new MimeBodyPart();
            StringBuilder stringBuilder = new StringBuilder();
            stringBuilder.append(text);
            stringBuilder.append("<img src=\"cid:customId\"/>");
            textPart.setContent(stringBuilder.toString(),"text/html;charset=UTF-8");


            //	邮件附件
            BodyPart filePart = new MimeBodyPart();
            filePart.setFileName(imageName+".jpg");

            //	提交附件文件
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                filePart.setDataHandler(
                        new DataHandler(
                                new ByteArrayDataSource(
                                        Files.readAllBytes(
                                                Paths.get(filePath)),
                                        "application/octet-stream")));
            }

            filePart.setHeader("Content-ID","customId");

            Multipart multipart = new MimeMultipart();
            multipart.addBodyPart(textPart);
            multipart.addBodyPart(filePart);

            //	将邮件装入信封
            message.setContent(multipart);

            Transport.send(message);
        } catch (AddressException e) {
            e.printStackTrace();
        } catch (MessagingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }


    public static void main(String[] args) {
        sendTxt("Hello","Hello","397035160@qq.com");
        List<String> files = new ArrayList<String>();
        files.add("D:\\myprojects\\admin_server_template\\logs\\spring.log");
        files.add("D:\\myprojects\\admin_server_template\\logs\\spring.log.2022-06-19.0.gz");
        sendFile("Hello","Hello",files,"397035160@qq.com");
        sendImage("Hello","Hello","D:\\myprojects\\admin_server_template\\src\\main\\resources\\static\\pic\\login.jpg","test","397035160@qq.com");
    }
}
